package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpCertificateRequireDAO;
import org.jsola.hr.dto.EmpCertificateRequireAddDTO;
import org.jsola.hr.dto.EmpCertificateRequireUpdateDTO;
import org.jsola.hr.entity.CompanyDO;
import org.jsola.hr.entity.EmpCertificateRequireDO;
import org.jsola.hr.query.EmpCertificateRequireQuery;
import org.jsola.hr.service.ICompanyService;
import org.jsola.hr.service.IEmpCertificateRequireService;
import org.jsola.hr.vo.EmpCertificateRequireListVO;
import org.jsola.hr.vo.EmpCertificateRequireVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrEmpCertificateRequireServiceImpl")
public class EmpCertificateRequireServiceImpl implements IEmpCertificateRequireService {

    @Autowired
    private IEmpCertificateRequireDAO empCertificateRequireDAO;

    @Autowired
    private ICompanyService companyService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpCertificateRequireVO save(EmpCertificateRequireAddDTO empCertificateRequireAddDTO, TokenUser tokenUser) {
        // 校验是否存在
        EmpCertificateRequireVO requireVO = findByCompanyAndType(
                empCertificateRequireAddDTO.getCompanyId(),
                empCertificateRequireAddDTO.getRequireType(),
                tokenUser.getSiteId());
        if (!Objects.isNull(requireVO)) {
            // 修改
            EmpCertificateRequireUpdateDTO requireUpdateDTO = empCertificateRequireAddDTO.to(EmpCertificateRequireUpdateDTO.class);
            requireUpdateDTO.setId(requireVO.getId());

            update(requireUpdateDTO, tokenUser);
            return selectById(requireVO.getId(), tokenUser.getSiteId());
        }
        // 转DO
        EmpCertificateRequireDO empCertificateRequireDO = empCertificateRequireAddDTO.to(EmpCertificateRequireDO.class);
        // 保存
        empCertificateRequireDO = save(empCertificateRequireDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empCertificateRequireDO.to(EmpCertificateRequireVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpCertificateRequireUpdateDTO empCertificateRequireUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpCertificateRequireDO empCertificateRequireDO = empCertificateRequireUpdateDTO.to(EmpCertificateRequireDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empCertificateRequireDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empCertificateRequireIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empCertificateRequireIds);
    }


    @Override
    public EmpCertificateRequireVO selectById(String empCertificateRequireId, String siteId) {
        EmpCertificateRequireDO empCertificateRequireDO = selectDOById(empCertificateRequireId, siteId);
        if (empCertificateRequireDO == null) {
            return null;
        }
        return empCertificateRequireDO.to(EmpCertificateRequireVO.class);
    }

    @Override
    public List<EmpCertificateRequireListVO> select(EmpCertificateRequireQuery empCertificateRequireQuery, String siteId) {
        List<EmpCertificateRequireDO> empCertificateRequireDOList = selectDO(empCertificateRequireQuery, siteId);
        if(CollectionUtils.isEmpty(empCertificateRequireDOList)) {
            return empCertificateRequireDOList == null ? null : new ArrayList<>();
        }
        return empCertificateRequireDOList.stream()
                .map(empCertificateRequireDO -> empCertificateRequireDO.to(EmpCertificateRequireListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpCertificateRequireQuery empCertificateRequireQuery, String siteId) {
        Example example = buildExample(empCertificateRequireQuery,siteId);
        return empCertificateRequireDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpCertificateRequireListVO> selectPage(EmpCertificateRequireQuery empCertificateRequireQuery, String siteId) {
        Example example = buildExample(empCertificateRequireQuery,siteId);
        Page<EmpCertificateRequireDO> page = empCertificateRequireDAO.selectPageByExample(example,
                empCertificateRequireQuery.getPageNo(),
                empCertificateRequireQuery.getPageSize());

        return page.to(EmpCertificateRequireListVO.class);
    }

    @Override
    public EmpCertificateRequireDO selectDOById(String empCertificateRequireId, String siteId) {
        return listById(empCertificateRequireId, siteId);
    }

    @Override
    public List<EmpCertificateRequireDO> selectDO(EmpCertificateRequireQuery empCertificateRequireQuery, String siteId) {
        Example example = buildExample(empCertificateRequireQuery,siteId);
        return empCertificateRequireDAO.selectByExample(example);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpCertificateRequireVO selectByCompanyAndType(String companyId, String requireType, TokenUser tokenUser) {
        EmpCertificateRequireVO requireVO = findByCompanyAndType(companyId, requireType, tokenUser.getSiteId());
        if (Objects.isNull(requireVO)) {
            CompanyDO company = companyService.selectDOById(companyId, tokenUser.getSiteId());
            if (Objects.isNull(company)) {
                return null;
            }
            // 没有初始化一条
            EmpCertificateRequireAddDTO requireAddDTO = new EmpCertificateRequireAddDTO();
            requireAddDTO.setCompanyId(companyId);
            requireAddDTO.setRequireType(requireType);

            requireVO = save(requireAddDTO, tokenUser);
        }
        return requireVO;
    }

    /**
     * 根据公司id和类型查询所需信息
     *
     * @param companyId   公司id
     * @param requireType 类型
     * @param siteId      站点
     * @return 所需信息
     */
    private EmpCertificateRequireVO findByCompanyAndType(String companyId, String requireType, String siteId) {
        Example example = new Example(EmpCertificateRequireDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("requireType", requireType)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);

        EmpCertificateRequireDO requireDO = empCertificateRequireDAO.selectOneByExample(example);
        if (Objects.isNull(requireDO)) {
            return null;
        }
        return requireDO.to(EmpCertificateRequireVO.class);
    }


    /**
     * 根据查询参数，构建example

     * @param empCertificateRequireQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpCertificateRequireQuery empCertificateRequireQuery, String siteId) {
        Example example = new Example(EmpCertificateRequireDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (empCertificateRequireQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", empCertificateRequireQuery.getCompanyId());
        }
        if (empCertificateRequireQuery.getRequireType() != null) {
            example.and().andEqualTo("requireType", empCertificateRequireQuery.getRequireType());
        }
        if (empCertificateRequireQuery.getType() != null) {
            example.and().andEqualTo("type", empCertificateRequireQuery.getType());
        }
        if (empCertificateRequireQuery.getName() != null) {
            example.and().andEqualTo("name", empCertificateRequireQuery.getName());
        }
        if (empCertificateRequireQuery.getLevel() != null) {
            example.and().andEqualTo("level", empCertificateRequireQuery.getLevel());
        }
        if (empCertificateRequireQuery.getNumber() != null) {
            example.and().andEqualTo("number", empCertificateRequireQuery.getNumber());
        }
        if (empCertificateRequireQuery.getStartTime() != null) {
            example.and().andEqualTo("startTime", empCertificateRequireQuery.getStartTime());
        }
        if (empCertificateRequireQuery.getEndTime() != null) {
            example.and().andEqualTo("endTime", empCertificateRequireQuery.getEndTime());
        }
        if (empCertificateRequireQuery.getRemark() != null) {
            example.and().andEqualTo("remark", empCertificateRequireQuery.getRemark());
        }
        // 排序
        ExampleKit.setExampleOrder(example,empCertificateRequireQuery.getOrders());
        return example;
    }
}




